<template>
  <div class="book-list">
    <!-- 显示书籍列表 -->
    <div class="book-table">
      <el-table :data="bookList" style="width: 100%" max-height="250">
        <el-table-column fixed prop="id" label="序号" width="150"></el-table-column>
        <el-table-column prop="title" label="书名" width="120"></el-table-column>
        <el-table-column prop="author" label="作者" width="120"></el-table-column>
        <el-table-column prop="copies" label="份数" width="120"></el-table-column>
        <el-table-column fixed="right" label="操作" width="120">
          <template slot-scope="scope">
            <el-button @click.native.prevent="deleteRow(scope.$index, bookList)" type="text" size="small">
              移除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <!-- 添加书籍表单 -->
    <div class="add-book-form">
      <el-form :model="newBook" ref="newBookForm" label-width="80px" class="book-form">
        <el-form-item prop="title" label="书名" :rules="[
          //required: 表示该字段是否为必填项，如果设置为 true，则表示该字段必须填写。
          //message: 当校验失败时显示的错误信息。
          //trigger: 触发校验的时机，可选值包括 'blur'（失去焦点时触发）、'change'（内容改变时触发）等。
          { required: true, message: '请输入书名', trigger: 'blur' }
        ]">
          <el-input v-model="newBook.title"></el-input>
        </el-form-item>
        <el-form-item prop="author" label="作者" :rules="[
          { required: true, message: '请输入作者', trigger: 'blur' }
        ]">
          <el-input v-model="newBook.author"></el-input>
        </el-form-item>
        <el-form-item prop="copies" label="份数" :rules="[
          { required: true, message: '请输入份数', trigger: 'blur' },
          { type: 'number', message: '份数必须为数字', trigger: 'blur' }
        ]">
          <el-input v-model="newBook.copies" type="number"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="addBook">添加书籍</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  name: 'BookList',
  data() {
    return {
      // 书籍列表数据
      bookList: [
        { "id": 1, "title": "Python编程", "author": "John Doe", "copies": 4 },
        { "id": 2, "title": "历史的秘密", "author": "Jane Smith", "copies": 3 },
        { "id": 3, "title": "宇宙探索", "author": "Emily Brown", "copies": 2 },
        { "id": 4, "title": "健康生活", "author": "Mike Johnson", "copies": 5 },
        { "id": 5, "title": "数学之美", "author": "Sarah Lee", "copies": 4 },
        { "id": 6, "title": "艺术欣赏", "author": "David White", "copies": 3 },
        { "id": 7, "title": "编程入门", "author": "Alice Green", "copies": 2 },
        { "id": 8, "title": "科幻世界", "author": "Frank Black", "copies": 5 },
        { "id": 9, "title": "自然奇观", "author": "Grace Blue", "copies": 4 },
        { "id": 10, "title": "音乐之声", "author": "Tom Red", "copies": 3 }
      ],
      // 添加书籍的表单数据
      newBook: {
        id: '',
        title: '',
        author: '',
        copies: ''
      }
    }
  },
  methods: {
    deleteRow(index, rows) {
      rows.splice(index, 1);
    },
    addBook() {
      //根据列表自己生成id
      const newId = this.bookList.length + 1;
      // 创建一个新的对象获取到新书籍的数据
      const newBookItem = { id: newId, title: this.newBook.title, author: this.newBook.author, copies: this.newBook.copies };
      this.bookList.push(newBookItem);
      this.newBook = { title: '', author: '', copies: '' };
      //将表单字段的值重置为初始值，同时清除验证状态
      this.$refs.newBookForm.resetFields();
    }
  }
}
</script>

<style scoped>
.book-list {
  display: flex;
  justify-content: space-around;
  margin-top: 20px;
}

.book-table {
  width: 50%;
}

.add-book-form {
  width: 40%;
}

.book-form {
  margin-top: 20px;
}
</style>